<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item label="专业编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="majoCode">
          <a-input v-model="model.majoCode" placeholder="请输入专业编码" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="专业名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="majoName">
          <a-input v-model="model.majoName" placeholder="请输入专业名称" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="专业简称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="majoAbbreviation">
          <a-input v-model="model.majoAbbreviation" placeholder="请输入专业简称" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="顺序序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNum">
          <a-input v-model="model.orderNum" placeholder="请输入顺序序号" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="父级专业ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pid">
          <j-tree-select
            ref="treeSelect"
            placeholder="请选择父级专业ID"
            v-model="model.pid"
            dict="cou_major,majo_name,id"
            pidField="pid"
            pidValue="0"
            hasChildField="has_child"
            >
          </j-tree-select>
        </a-form-model-item>
        <a-form-model-item label="是否有子节点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hasChild">
          <a-input v-model="model.hasChild" placeholder="请输入是否有子节点" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="专业门类，来自字典码majo_ttype，例如：法学门类分为法学类和社会学类等专业大类," :labelCol="labelCol" :wrapperCol="wrapperCol" prop="majoTtype">
          <a-input v-model="model.majoTtype" placeholder="请输入专业门类，来自字典码majo_ttype，例如：法学门类分为法学类和社会学类等专业大类," ></a-input>
        </a-form-model-item>
        <a-form-model-item label="专业大类(与父类同值)，例如：法学门类分为法学类和社会学类等专业大类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="majorCategory">
          <a-input v-model="model.majorCategory" placeholder="请输入专业大类(与父类同值)，例如：法学门类分为法学类和社会学类等专业大类" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="学位门类：无、哲学、经济学、法学等" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="degreeCategory">
          <a-input v-model="model.degreeCategory" placeholder="请输入学位门类：无、哲学、经济学、法学等" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="学位证等级：(字典码)无、 本科 研究生" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="academicDegreeRank">
          <j-dict-select-tag type="list" v-model="model.academicDegreeRank"  dictCode="academic_degree_rank" placeholder="请选择学位证等级：(字典码)无、 本科 研究生" />
        </a-form-model-item>
        <a-form-model-item label="毕业文凭(毕业证)：(字典码)高中、高职、大专、本科、研究生" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="graduateDiploma">
          <j-dict-select-tag type="list" v-model="model.graduateDiploma"  dictCode="graduate_diploma" placeholder="请选择毕业文凭(毕业证)：(字典码)高中、高职、大专、本科、研究生" />
        </a-form-model-item>
        <a-form-model-item label="教育等级：(字典码)，高职、大专 本科 研究生等" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teachingRank">
          <j-dict-select-tag type="list" v-model="model.teachingRank"  dictCode="teaching_rank" placeholder="请选择教育等级：(字典码)，高职、大专 本科 研究生等" />
        </a-form-model-item>
        <a-form-model-item label="学制，修业年限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lengthOfSchooling">
          <j-dict-select-tag type="list" v-model="model.lengthOfSchooling"  dictCode="" placeholder="请选择学制，修业年限" />
        </a-form-model-item>
        <a-form-model-item label="状态 1正常 2取消" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="majoStatus">
          <j-dict-select-tag type="list" v-model="model.majoStatus"  dictCode="majo_status" placeholder="请选择状态 1正常 2取消" />
        </a-form-model-item>
        <a-form-model-item label="专业描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="majoDescription">
          <a-input v-model="model.majoDescription" placeholder="请输入专业描述" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="是否启用：1是(启用) 0否(停用)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">
          <j-dict-select-tag type="list" v-model="model.izOpen"  dictCode="" placeholder="请选择是否启用：1是(启用) 0否(停用)" />
        </a-form-model-item>
        <a-form-model-item label="租户ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tenantId">
          <a-input v-model="model.tenantId" placeholder="请输入租户ID" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="删除状态：1是(已删除) 0否(正常)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delFlag">
          <a-input-number v-model="model.delFlag" placeholder="请输入删除状态：1是(已删除) 0否(正常)" style="width: 100%" />
        </a-form-model-item>
        
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  export default {
    name: "CouMajorModal",
    components: { 
    },
    data () {
      return {
        title:"操作",
        width:800,
        visible: false,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
           orderNum: [
              { required: true, message: '请输入顺序序号!'},
           ],
           tenantId: [
              { required: true, message: '请输入租户ID!'},
           ],
        },
        url: {
          add: "/coumajor/couMajor/add",
          edit: "/coumajor/couMajor/edit",
        },
        expandedRowKeys:[],
        pidField:"pid"
     
      }
    },
    created () {
       //备份model原始值
       this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (obj) {
        this.modelDefault.pid=''
        this.edit(Object.assign(this.modelDefault , obj));
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate()
      },
      handleOk () {
        const that = this;
        // 触发表单验证
       this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
             if(this.model.id && this.model.id === this.model[this.pidField]){
              that.$message.warning("父级节点不能选择自己");
              that.confirmLoading = false;
              return;
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false
          }
        })
      },
      handleCancel () {
        this.close()
      },
      submitSuccess(formData,flag){
        if(!formData.id){
          let treeData = this.$refs.treeSelect.getCurrTreeData()
          this.expandedRowKeys=[]
          this.getExpandKeysByPid(formData[this.pidField],treeData,treeData)
          this.$emit('ok',formData,this.expandedRowKeys.reverse());
        }else{
          this.$emit('ok',formData,flag);
        }
      },
      getExpandKeysByPid(pid,arr,all){
        if(pid && arr && arr.length>0){
          for(let i=0;i<arr.length;i++){
            if(arr[i].key==pid){
              this.expandedRowKeys.push(arr[i].key)
              this.getExpandKeysByPid(arr[i]['parentId'],all,all)
            }else{
              this.getExpandKeysByPid(pid,arr[i].children,all)
            }
          }
        }
      }
      
      
    }
  }
</script>